package com.sxwl.auth.config;

import com.sxwl.common.pojo.UserInfo;
import com.sxwl.common.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
 * @author reading
 * @Description TODO
 * @Date 2020/4/7 13:58
 */
@Configuration
@Slf4j
public class LoginFilter implements GlobalFilter, Ordered {
    @Autowired
    private FilterProperties properties;
    @Autowired
    private JwtProperties jwtProperties;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~登录过滤器开始执行了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        ServerHttpRequest request = exchange.getRequest();
        if (isAllowPath(request.getPath().value())) {
            return chain.filter(exchange);
        }
        HttpHeaders headers = request.getHeaders();
        String token = null;
        try {
            token = headers.get("token").get(0);
        } catch (Exception e) {
            e.printStackTrace();
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        // 校验通过什么都不做，即放行
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            ServerHttpRequest host = request.mutate().headers(httpHeaders -> {
                httpHeaders.add("uid", String.valueOf(infoFromToken.getId()));
            }).build();
            ServerWebExchange build = exchange.mutate().request(host).build();
            return chain.filter(build);
        } catch (Exception e) {
            e.printStackTrace();
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
    }
    @Override
    public int getOrder() {
        return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1;
    }
    private boolean isAllowPath(String requestURI) {
        // 定义一个标记
        boolean flag = false;
        // 遍历允许访问的路径
        for (String path : this.properties.getAllowPaths()) {
            // 然后判断是否是符合
            if(path.endsWith("/**")){
                if(requestURI.startsWith(path.substring(0, path.indexOf("*")))){
                    flag=true;
                }
            }else{
                if(path.equals(requestURI)){
                    flag=true;
                }
            }
        }
        return flag;
    }
}
